﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Web.BML.AnalysisData;
using Web.BML.CoreData;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using Web.ViewModel.CoreData;

namespace Web.Service
{
    public  class SysUserService : BaseService
    {
        
        private SysUserOp sysUserOp = SysUserOp.GetInstance();

        private static SysUserService instance;

        private SysUserService()
        {

        }

        public static SysUserService GetInstance()
        {
            if (instance == null)
            {
                instance = new SysUserService();
            }
            return instance;
        }



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public IServiceResult Login(string account,string password)
        {
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
            {
                return Result(ResultType.ParamIsNull);
            }
            SysUser sysUser = sysUserOp.QueryUser(account, password);
            if (sysUser == null)
            {
                return Result(ResultType.AccountLoginFaile);
            }
            HttpContext.Current.Session["SYSUSER"] = sysUser;
            return Result(ResultType.Success);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IServiceResult ResetPassword(Guid id)
        {
            SysUser sysUser = sysUserOp.QueryUser(id);
            sysUser.Password = "123456";
            sysUserOp.ModifUser(sysUser);
            return Result(ResultType.Success);
        }

        /// <summary>
        /// 获取所有系统用户
        /// </summary>
        /// <returns></returns>
        public List<SysUserModel> GetAllSysUser()
        {
             return sysUserOp.QueryAllUser().Select(m => new SysUserModel().ConvertSysUserModel(m)).ToList();
        }

        /// <summary>
        /// 添加系统用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IServiceResult AddSysUser(SysUserModel model)
        {
            if (string.IsNullOrEmpty(model.Account) || string.IsNullOrEmpty(model.Password) ||
                string.IsNullOrEmpty(model.Phone) || string.IsNullOrEmpty(model.Name))
            {
                return Result(ResultType.ParamIsNull, "请检查输入所有必填项");
            }
            sysUserOp.CreateUser(new SysUser()
             {
                 Account = model.Account,
                 Id = Guid.NewGuid(),
                 Password = model.Password,
                 Name = model.Name,
                 Phone = model.Phone
             });
            return Result(ResultType.Success);
        }

    }
}